<?php
require_once("mysql.inc.php");

if(isset($_GET["a"])) {
	
	if($_GET["a"] == "li") {
		mostLiked();
	}
	elseif($_GET["a"] == "le") {
		mostLend();
	}
}

function mostLiked() {

	$result = mysql_query("SELECT * FROM `books` WHERE `likes`>0 ORDER BY `likes` LIMIT 10") or die(mysql_error());
	$num = mysql_num_rows($result);
	
	if($num > 10) {
		$max = 10;
	}
	else {
		$max = $num;
	}
	
	$no_info = "Niet bekend";
	$i = $max;
	$j = 1;
	
	//create new dom document
	$doc = new DomDocument('1.0');
	$root = $doc->createElement("results");
	$doc->appendChild($root);
	
	//create first element containing amount of results
	$n = $doc->createElement("num");
	$n->appendChild($doc->createTextNode($max));
	$root->appendChild($n);
	
	while(($fetch = mysql_fetch_array($result)) && $j <= $max) {
		$t = $doc->createElement("book$i");
		
		$title = $doc->createElement("title$i");
		if(!empty($fetch["title"])) {
			$title->appendChild($doc->createTextNode($fetch["title"]));
		}			
		else {
			$title->appendChild($doc->createTextNode($no_info));
		}		
		$t->appendChild($title);
		
		$author = $doc->createElement("author$i");
		if(!empty($fetch["author"])) {
			$author->appendChild($doc->createTextNode($fetch["author"]));
		}
		else {
			$author->appendChild($doc->createTextNode($no_info));
		}		
		$t->appendChild($author);
		
		$likes = $doc->createElement("likes$i");
		if(!empty($fetch["likes"])) {
			$likes->appendChild($doc->createTextNode($fetch["likes"]));
		}
		else {
			$likes->appendChild($doc->createTextNode($no_info));
		}
		$t->appendChild($likes);
		
		$root->appendChild($t);	
		
		$i--;
		$j++;
		
	}
	echo $doc->saveXML();
}

function mostLend() {

	$result = mysql_query("SELECT * FROM `books` WHERE `lend`>0 ORDER BY `lend` LIMIT 10") or die(mysql_error());
	$num = mysql_num_rows($result);
	
	if($num > 10) {
		$max = 10;
	}
	else {
		$max = $num;
	}
	
	$no_info = "Niet bekend";
	$i = $max;
	$j = 1;
	
	//create new dom document
	$doc = new DomDocument('1.0');
	$root = $doc->createElement("results");
	$doc->appendChild($root);
	
	//create first element containing amount of results
	$n = $doc->createElement("num");
	$n->appendChild($doc->createTextNode($max));
	$root->appendChild($n);
	
	while(($fetch = mysql_fetch_array($result)) && $j <= $max) {
		$t = $doc->createElement("book$i");
		
		$title = $doc->createElement("title$i");
		if(!empty($fetch["title"])) {
			$title->appendChild($doc->createTextNode($fetch["title"]));
		}			
		else {
			$title->appendChild($doc->createTextNode($no_info));
		}		
		$t->appendChild($title);
		
		$author = $doc->createElement("author$i");
		if(!empty($fetch["author"])) {
			$author->appendChild($doc->createTextNode($fetch["author"]));
		}
		else {
			$author->appendChild($doc->createTextNode($no_info));
		}		
		$t->appendChild($author);
		
		$lend = $doc->createElement("lend$i");
		if(!empty($fetch["lend"])) {
			$lend->appendChild($doc->createTextNode($fetch["lend"]));
		}
		else {
			$lend->appendChild($doc->createTextNode($no_info));
		}
		$t->appendChild($lend);
		
		$root->appendChild($t);	
		
		$i--;
		$j++;	
	}
	
echo $doc->saveXML();
}
?>